package JDBC;

import java.sql.*;
import java.util.Scanner;

class Admin {
    private int adminId;
    private String adminAccount;
    private String adminPassword;
    public Admin() {
    }
    public void Updatename()throws ClassNotFoundException, SQLException{
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.用户信息和url
        //useUnicode=true&characterEncoding=utf8&useSSL=true
        //useUnicode=true&characterEncoding=utf8&useSSL=false  SQL版本大于connect版本，就要设置成false
        String url = "jdbc:mysql://localhost:3306/外卖平台管理系统?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "Elysia";
        //3.链接成功，数据库对象
        //connection代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //4.执行sql的对象
        Statement statement = connection.createStatement();
        System.out.println("请选择要删除的数据");
        System.out.println("  1.商家");
        System.out.println("  2.用户");
        System.out.println("  3.骑手");
        System.out.println("  4.商品");
        System.out.println("  0.返回");
        int button; String id;String name;
        Scanner sc = new Scanner(System.in);
        button = sc.nextInt();
        String sql=null;
        ResultSet resultSet = null;
        switch (button) {
            case 1:
                sql="select *from  merchants";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "merchant_name="+resultSet.getObject("merchant_name")+"   "
                            + "merchant_account=" + resultSet.getObject("merchant_account") + "   "
                            + "merchant_password=" + resultSet.getObject("merchant_password")+ "   "
                            + "merchant_phone="+resultSet.getObject("merchant_phone")+"   "
                            +"merchant_address="+resultSet.getObject("merchant_address"));
                }
                System.out.println("请输入要修改密码的id");
                sc.nextLine();
                id = sc.nextLine();
                System.out.println("请输入修改后的名字");
                name=sc.nextLine();
                sql = "update  merchants set merchant_name=\'"+name+"\' where merchant_id=\'"+id+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM merchants";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "merchant_name="+resultSet.getObject("merchant_name")+"   "
                            + "merchant_account=" + resultSet.getObject("merchant_account") + "   "
                            + "merchant_password=" + resultSet.getObject("merchant_password")+ "   "
                            + "merchant_phone="+resultSet.getObject("merchant_phone")+"   "
                            +"merchant_address="+resultSet.getObject("merchant_address"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 2:
                sql = "SELECT * FROM customers";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("customer_id=" + resultSet.getObject("customer_id") + "   "
                            + "customer_name="+resultSet.getObject("customer_name")+"   "
                            + "customer_account=" + resultSet.getObject("customer_account") + "   "
                            + "customer_password=" + resultSet.getObject("customer_password")+ "   "
                            + "customer_phone="+resultSet.getObject("customer_phone")+"   "
                            +"customer_address="+resultSet.getObject("customer_address"));
                }
                System.out.println("请输入要修改密码的id");
                sc.nextLine();
                id = sc.nextLine();
                System.out.println("请输入修改后的名字");
                name=sc.nextLine();
                sql = "update  customers set customer_name=\'"+name+"\' where customer_id=\'"+id+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM customers";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("customer_id=" + resultSet.getObject("customer_id") + "   "
                            + "customer_name="+resultSet.getObject("customer_name")+"   "
                            + "customer_account=" + resultSet.getObject("customer_account") + "   "
                            + "customer_password=" + resultSet.getObject("customer_password")+ "   "
                            + "customer_phone="+resultSet.getObject("customer_phone")+"   "
                            +"customer_address="+resultSet.getObject("customer_address"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 3:
                sql = "SELECT * FROM riders";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("rider_id=" + resultSet.getObject("rider_id") + "   "
                            + "rider_name="+resultSet.getObject("rider_name")+"   "
                            + "rider_account=" + resultSet.getObject("rider_account") + "   "
                            + "rider_password=" + resultSet.getObject("rider_password")+ "   "
                            + "rider_phone="+resultSet.getObject("rider_phone")+"   "
                            +"rider_status="+resultSet.getObject("rider_status"));
                }
                System.out.println("请输入要修改密码的id");
                sc.nextLine();
                id = sc.nextLine();
                System.out.println("请输入修改后的名字");
                name=sc.nextLine();
                sql = "update  riders set rider_name=\'"+name+"\' where rider_id=\'"+id+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM riders";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("rider_id=" + resultSet.getObject("rider_id") + "   "
                            + "rider_name="+resultSet.getObject("rider_name")+"   "
                            + "rider_account=" + resultSet.getObject("rider_account") + "   "
                            + "rider_password=" + resultSet.getObject("rider_password")+ "   "
                            + "rider_phone="+resultSet.getObject("rider_phone")+"   "
                            +"rider_status="+resultSet.getObject("rider_status"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 4:
                sql = "SELECT * FROM goods";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("goods_id=" + resultSet.getObject("goods_id") + "   "
                            + "goods_name="+resultSet.getObject("goods_name")+"   "
                            +"merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "goods_price=" + resultSet.getObject("goods_price")+ "   "
                            +"goods_limit_num="+resultSet.getObject("goods_limit_num"));
                }
                System.out.println("请输入要修改密码的id");
                sc.nextLine();
                id = sc.nextLine();
                System.out.println("请输入修改后的名字");
                name=sc.nextLine();
                sql = "update  goods set goods_name=\'"+name+"\' where goods_id=\'"+id+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM goods";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("goods_id=" + resultSet.getObject("goods_id") + "   "
                            + "goods_name="+resultSet.getObject("goods_name")+"   "
                            +"merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "goods_price=" + resultSet.getObject("goods_price")+ "   "
                            +"goods_limit_num="+resultSet.getObject("goods_limit_num"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            default:
                break;
        }
    }
    public void Updatepassword()throws ClassNotFoundException, SQLException{
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");//固定写法，用于加载驱动
        //2.用户信息和url
        //useUnicode=true&characterEncoding=utf8&useSSL=true
        //useUnicode=true&characterEncoding=utf8&useSSL=false  SQL版本大于connect版本，就要设置成false
        String url = "jdbc:mysql://localhost:3306/外卖平台管理系统?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "Elysia";
        //3.链接成功，数据库对象
        //connection代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //4.执行sql的对象
        Statement statement = connection.createStatement();
        String admin_account;String admin_password;
        Scanner sc = new Scanner(System.in);
        String sql=null;
        ResultSet resultSet = null;
        sql = "SELECT * FROM admin";
        resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
        while (resultSet.next()) {
            System.out.println("admin_id=" + resultSet.getObject("admin_id") + "   "
                    + "admin_account=" + resultSet.getObject("admin_account") + "   "
                    + "admin_password=" + resultSet.getObject("admin_password"));
        }
        System.out.println("请输入要修改密码的管理员id");
        admin_account = sc.nextLine();
        System.out.println("请输入修改后的管理员密码");
       //sc.nextLine();
        admin_password=sc.nextLine();
        sql = "update admin set admin_password=\'"+admin_password+"\'where admin_id=\'"+admin_account+"\'";
        statement.executeUpdate(sql);
        sql = "SELECT * FROM admin";
        resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
        while (resultSet.next()) {
            System.out.println("admin_id=" + resultSet.getObject("admin_id") + "   "
                    + "admin_account=" + resultSet.getObject("admin_account") + "   "
                    + "admin_password=" + resultSet.getObject("admin_password"));
        }
        resultSet.close();
        statement.close();
        connection.close();
    }


    public   void Deletedata()throws ClassNotFoundException, SQLException  {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");//固定写法，用于加载驱动
        //2.用户信息和url
        //useUnicode=true&characterEncoding=utf8&useSSL=true
        //useUnicode=true&characterEncoding=utf8&useSSL=false  SQL版本大于connect版本，就要设置成false
        String url = "jdbc:mysql://localhost:3306/外卖平台管理系统?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "Elysia";
        //3.链接成功，数据库对象
        //connection代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //4.执行sql的对象
        Statement statement = connection.createStatement();
        //5。执行sql的对象去执行sql，可能存在结果，查看返
        System.out.println("请选择要删除的数据");
        System.out.println("  1.管理员");
        System.out.println("  2.商家");
        System.out.println("  3.用户");
        System.out.println("  4.骑手");
        System.out.println("  5.订单");
        System.out.println("  6.商品");
        System.out.println("  0.返回");
        int button;String choice;
        Scanner sc = new Scanner(System.in);
        button = sc.nextInt();
        String sql=null;
        ResultSet resultSet = null;
        switch (button) {
            case 1:
               sql = "SELECT * FROM admin";
                 resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("admin_id=" + resultSet.getObject("admin_id") + "   "
                            + "admin_account=" + resultSet.getObject("admin_account") + "   "
                            + "admin_password=" + resultSet.getObject("admin_password"));
                }
               System.out.println("请输入要删除的管理员id");

                choice = sc.nextLine();
                choice = sc.nextLine();
                sql = "delete from admin where admin_id=\'"+choice+"\'";
                 statement.executeUpdate(sql);
                 sql = "SELECT * FROM admin";
                 resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("admin_id=" + resultSet.getObject("admin_id") + "   "
                            + "admin_account=" + resultSet.getObject("admin_account") + "   "
                            + "admin_password=" + resultSet.getObject("admin_password"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 2:
                sql = "SELECT * FROM merchants";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "merchant_name="+resultSet.getObject("merchant_name")+"   "
                            + "merchant_account=" + resultSet.getObject("merchant_account") + "   "
                            + "merchant_password=" + resultSet.getObject("merchant_password")+ "   "
                            + "merchant_phone="+resultSet.getObject("merchant_phone")+"   "
                            +"merchant_address="+resultSet.getObject("merchant_address"));
                }
                System.out.println("请输入要删除的商家id");
                choice = sc.nextLine();
                choice = sc.nextLine();
                sql = "delete from merchant_order where merchant_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from merchants where merchant_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM merchants";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "merchant_name="+resultSet.getObject("merchant_name")+"   "
                            + "merchant_account=" + resultSet.getObject("merchant_account") + "   "
                            + "merchant_password=" + resultSet.getObject("merchant_password")+ "   "
                            + "merchant_phone="+resultSet.getObject("merchant_phone")+"   "
                            +"merchant_address="+resultSet.getObject("merchant_address"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 3:
                sql = "SELECT * FROM customers";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("customer_id=" + resultSet.getObject("customer_id") + "   "
                            + "customer_name="+resultSet.getObject("customer_name")+"   "
                            + "customer_account=" + resultSet.getObject("customer_account") + "   "
                            + "customer_password=" + resultSet.getObject("customer_password")+ "   "
                            + "customer_phone="+resultSet.getObject("customer_phone")+"   "
                            +"customer_address="+resultSet.getObject("customer_address"));
                }
                System.out.println("请输入要删除的用户id");
                choice = sc.nextLine();
                choice = sc.nextLine();
                sql = "delete from customer_order where customer_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from customers where customer_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM customers";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("customer_id=" + resultSet.getObject("customer_id") + "   "
                            + "customer_name="+resultSet.getObject("customer_name")+"   "
                            + "customer_account=" + resultSet.getObject("customer_account") + "   "
                            + "customer_password=" + resultSet.getObject("customer_password")+ "   "
                            + "customer_phone="+resultSet.getObject("customer_phone")+"   "
                            +"customer_address="+resultSet.getObject("customer_address"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 4:
                sql = "SELECT * FROM riders";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("rider_id=" + resultSet.getObject("rider_id") + "   "
                            + "rider_name="+resultSet.getObject("rider_name")+"   "
                            + "rider_account=" + resultSet.getObject("rider_account") + "   "
                            + "rider_password=" + resultSet.getObject("rider_password")+ "   "
                            + "rider_phone="+resultSet.getObject("rider_phone")+"   "
                            +"rider_status="+resultSet.getObject("rider_status"));
                }
                System.out.println("请输入要删除的骑手id");
                choice = sc.nextLine();
                choice = sc.nextLine();
                sql = "delete from rider_order where rider_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from riders where rider_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM riders";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("rider_id=" + resultSet.getObject("rider_id") + "   "
                            + "rider_name="+resultSet.getObject("rider_name")+"   "
                            + "rider_account=" + resultSet.getObject("rider_account") + "   "
                            + "rider_password=" + resultSet.getObject("rider_password")+ "   "
                            + "rider_phone="+resultSet.getObject("rider_phone")+"   "
                            +"rider_status="+resultSet.getObject("rider_status"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 5:
                sql = "SELECT * FROM orders";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("order_id=" + resultSet.getObject("order_id") + "   "
                            + "start_time="+resultSet.getObject("start_time")+"   "
                            + "end_time=" + resultSet.getObject("end_time") + "   "
                            +"order_status="+resultSet.getObject("order_status"));
                }
                System.out.println("请输入要删除的订单id");
                choice = sc.nextLine();
                choice = sc.nextLine();
                sql = "delete from merchant_order where order_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from customer_order where order_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from rider_order where order_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from goods_order where order_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from orders where order_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM orders";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("order_id=" + resultSet.getObject("order_id") + "   "
                            + "start_time="+resultSet.getObject("start_time")+"   "
                            + "end_time=" + resultSet.getObject("end_time") + "   "
                            + "total_cost=" + resultSet.getObject("total_cost")+ "   "
                            +"order_status="+resultSet.getObject("order_status"));
                }
                break;
            case 6:
                  sql = "SELECT * FROM goods";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    System.out.println("goods_id=" + resultSet.getObject("goods_id") + "   "
                            + "goods_name="+resultSet.getObject("goods_name")+"   "
                            +"merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "goods_price=" + resultSet.getObject("goods_price")+ "   "
                            +"goods_limit_num="+resultSet.getObject("goods_limit_num"));
                }
                System.out.println("请输入要删除的商品id");
                choice = sc.nextLine();
                choice = sc.nextLine();
                sql = "delete from goods_order where goods_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "delete from goods where goods_id=\'"+choice+"\'";
                statement.executeUpdate(sql);
                sql = "SELECT * FROM goods";
                resultSet = statement.executeQuery(sql);//返回结果集，里面封装查询的结果
                while (resultSet.next()) {
                    System.out.println("goods_id=" + resultSet.getObject("goods_id") + "   "
                            + "goods_name="+resultSet.getObject("goods_name")+"   "
                            +"merchant_id=" + resultSet.getObject("merchant_id") + "   "
                            + "goods_price=" + resultSet.getObject("goods_price")+ "   "
                            +"goods_limit_num="+resultSet.getObject("goods_limit_num"));
                }
                resultSet.close();
                statement.close();
                connection.close();
                break;
            case 0:
                break;
            default:
                System.out.println("输入有误，请重新输入");
                break;
        }

    }


}

public class Main {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        String choice;
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("请输入你的选择");
            System.out.println("    1.注册");
            System.out.println("    2.登录");
            System.out.println("    0.退出");
            choice = sc.nextLine();
            switch (choice) {
                case "1":
                    RegistIdentity();
                    break;
                case "2":
                    break;
                case "0":
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }


        }




    }
    public static void RegistIdentity() throws SQLException, ClassNotFoundException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.用户信息和url
        //useUnicode=true&characterEncoding=utf8&useSSL=true
        //useUnicode=true&characterEncoding=utf8&useSSL=false  SQL版本大于connect版本，就要设置成false
        String url = "jdbc:mysql://localhost:3306/外卖平台管理系统?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "Elysia";
        //3.链接成功，数据库对象
        //connection代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //4.执行sql的对象
        Statement statement = connection.createStatement();
        String sql = null;
        ResultSet resultSet = null;
        System.out.println("请输入你的选择");
        System.out.println("    1.管理员");
        System.out.println("    2.用户");
        System.out.println("    3.骑手");
        System.out.println("    4.商家");
        System.out.println("    0.返回");
        String Button;String name;String address;
        String phone;int n;
        String account; String password1;
        Scanner sc = new Scanner(System.in);
        Button = sc.nextLine();
        switch (Button) {
            case "1":
                System.out.println("请输入账号");
                account = sc.nextLine();
                System.out.println("请输入密码");
                password1 = sc.nextLine();
                sql="insert into admin (admin_account,admin_password)values (\'"+account+"\',\'"+password1+"\')";
                n= statement.executeUpdate(sql);
               if (n>0)
               {
                   System.out.println("注册成功");
               }
               else
               {
                   System.out.println("注册失败");
               }
                break;
            case "2":
                System.out.println("请输入名字");
                name=sc.nextLine();
                System.out.println("请输入账号");
                account = sc.nextLine();
                System.out.println("请输入密码");
                password1 = sc.nextLine();
                System.out.println("请输入手机号");
                phone = sc.nextLine();
                System.out.println("请输入地址");
                address = sc.nextLine();
                sql="insert into customers(customer_name,customer_account,customer_password,customer_phone,customer_address) values (\'"+name+"\',\'"+account+"\',\'"+password1+"\',\'"+phone+"\',\'"+address+"\')";
                 n= statement.executeUpdate(sql);
                if (n>0)
                {
                    System.out.println("注册成功");
                }
                else
                {
                    System.out.println("注册失败");
                }
                break;
            case "3":
                System.out.println("请输入名字");
                name=sc.nextLine();
                System.out.println("请输入账号");
                account = sc.nextLine();
                System.out.println("请输入密码");
                password1 = sc.nextLine();
                System.out.println("请输入手机号");
                phone = sc.nextLine();
                sql="insert into riders(rider_name,rider_account,rider_password,rider_phone) values (\'"+name+"\',\'"+account+"\',\'"+password1+"\',\'"+phone+"\')";
                n= statement.executeUpdate(sql);
                if (n>0)
                {
                    System.out.println("注册成功");
                }
                else
                {
                    System.out.println("注册失败");
                }
                break;
            case "4":
                System.out.println("请输入名字");
                name=sc.nextLine();
                System.out.println("请输入账号");
                account = sc.nextLine();
                System.out.println("请输入密码");
                password1 = sc.nextLine();
                System.out.println("请输入手机号");
                phone = sc.nextLine();
                System.out.println("请输入地址");
                address = sc.nextLine();
                sql="insert into merchants(merchant_name,merchant_account,merchant_password,merchant_phone,merchant_address) values (\'"+name+"\',\'"+account+"\',\'"+password1+"\',\'"+phone+"\',\'"+address+"\')";
                n= statement.executeUpdate(sql);
                if (n>0)
                {
                    System.out.println("注册成功");
                }
                else
                {
                    System.out.println("注册失败");
                }
                break;

            case "0":
                return;
            default:
                System.out.println("输入有误，请重新输入");
                break;
        }


    }
}